using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace QFramework.FlappyBird
{
    public enum GameStates
    {
        NotStart,
        Started,
        GameOver
    }

    public class FlappyBird : Architecture<FlappyBird>
    {
        public static BindableProperty<GameStates> GameState = new BindableProperty<GameStates>(GameStates.NotStart);

        public static BindableProperty<int> Score = new BindableProperty<int>();

        public static BindableProperty<int> BestScore = new BindableProperty<int>();
        protected override void Init()
        {
            GameState.RegisterWithInitValue(state =>
            {
                if(state == GameStates.NotStart)
                {
                    Score.Value = 0;
                }
                else if(state == GameStates.Started)
                {
                    Score.Value = 0;
                    Time.timeScale = 1;
                }
                else
                {
                    UIKit.OpenPanel<UIGameOver>();
                    Time.timeScale = 0;
                }
            });

            BestScore.Value = PlayerPrefs.GetInt(nameof(BestScore));
            BestScore.Register(bestscore =>
            {
                PlayerPrefs.SetInt(nameof(BestScore), bestscore);
            });

            Score.RegisterWithInitValue(score =>
            {
                if(score > BestScore.Value)
                {
                    BestScore.Value = score;
                }
            });

        }

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
        public static void InitFramework()
        {
            ResKit.Init();
            var _ = FlappyBird.Interface;
        }
    }
}

